<template>
    <h3>添加优惠券信息</h3>
    <table>
        <tr>
            <td>优惠券标题</td>
            <td>
                <input type="text" placeholder="请输入优惠券标题" v-model="info.Coupon_Title"/>
            </td>
        </tr>
        <tr>
            <td>优惠券颜色</td>
            <td>
                <input type="text" placeholder="请输入优惠券颜色" v-model="info.Coupon_Color"/>
            </td>
        </tr>
        <tr>
            <td>优惠券剩余库存</td>
            <td>
                <input type="text" placeholder="请输入优惠券剩余库存" v-model="info.Coupon_Num"/>
            </td>
        </tr>
        <tr>
            <td>开始有效期</td>
            <td>
                <input type="text" placeholder="请输入开始有效期" v-model="info.Coupon_StartTime"/>
            </td>
        </tr>
        <tr>
            <td>结束有效期</td>
            <td>
                <input type="text" placeholder="请输入结束有效期" v-model="info.Coupon_EndTime"/>
            </td>
        </tr>
        <tr>
            <td>使用须知</td>
            <td>
                <textarea cols="20" rows="4" placeholder="请输入使用须知" v-model="info.Coupon_Precautions"></textarea>
            </td>
        </tr>
        <tr>
            <td>客服电话</td>
            <td>
                <input type="text" placeholder="请输入优惠卷客服电话" v-model="info.Coupon_Phone"/>
            </td>
        </tr>
        <tr>
            <td>门店名称</td>
            <td>
                <select v-model="info.Shop_Id">
                    <option value="0">--请选择--</option>
                    <option v-for="item in shopInfo" :value="item.Shop_Id   ">{{ item.Shop_Name }}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>优惠券类型</td>
            <td>
                <select v-model="info.CouponType_Id">
                    <option value="0">--请选择--</option>
                    <option v-for="item in typeinfo" :value="item.CouponType_Id" >{{ item.CouponType_Name }}</option>
                </select>
            </td>
        </tr>
        <tr v-show="info.CouponType_Id==1">
            <td>折扣</td>
            <td>
                <input type="text" placeholder="请输入折扣" v-model="info.Coupon_Discount"/>
            </td>
        </tr>
        <tr  v-show="info.CouponType_Id==2">
            <td>代金金额</td>
            <td>
                <input type="text" placeholder="请输入代金金额" v-model="info.Coupon_Money"/>
            </td>
        </tr>
        <tr v-show="info.CouponType_Id==3">
            <td>兑换商品名称</td>
            <td>
                <input type="text" placeholder="请输入兑换商品名称" v-model="info.Coupon_CouponName"/>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" value="添加" class="btn btn-success" @click="AddCoupon"/>
            </td>
        </tr>
    </table>




</template>
<script setup lang="ts">
import axios from 'axios';
import { useRouter,useRoute } from 'vue-router';
import { onMounted,ref } from 'vue';

const router=useRouter();
onMounted(()=>{
    GetShop();
    GetCouponType();
})
//创建响应式数据用于保存优惠信息
const info=ref({
  "Coupon_Id": 0,
  "Coupon_Title": "",
  "Coupon_Color": "",
  "Coupon_Num": 0,
  "Coupon_Limited": 0,
  "Coupon_StartTime": "",
  "Coupon_EndTime": "",
  "Coupon_Precautions": "",
  "Coupon_Phone": "",
  "Coupon_Discount": 0,
  "Coupon_Money": 0,
  "Coupon_CouponName": "",
  "CouponType_Id": 0,
  "Shop_Id": 0
})
//创建响应式数据用于保存门店信息
const shopInfo=ref([{
    Shop_Id:0,
    Shop_Name:""
}])
//获取门店信息
const GetShop=()=>{
    axios.get("https://localhost:7035/api/Coupon/ShowShop")
    .then(res=>{
        shopInfo.value=res.data
    })
}
//创建响应式数据用于保存优惠券类型信息
const typeinfo=ref([{
    CouponType_Id:0,
    CouponType_Name:""
}])
//加载优惠卷类型信息
const GetCouponType=()=>{
    axios.get("https://localhost:7035/api/Coupon/ShowCouponType")
    .then(res=>{
        typeinfo.value=res.data
    })
}

//完成数据保存
const AddCoupon=()=>{
    if(info.value.Coupon_Title==""){
        alert("请输入优惠券标题")
        return;
    }
    if(info.value.Coupon_Num>0&&info.value.Coupon_Num<1000){
        alert("库存量必须大于0小于1000")
        return;
    }
    if(info.value.Coupon_Precautions.length<5){
        alert("优惠券须知必须大于5");
        return;
    }
    var zzPhone=/^1[3-9]\d{9}$/
    if(!zzPhone.test(info.value.Coupon_Phone)){
        alert("手机号格式不正确")
        return;
    }
    if(info.value.Shop_Id==0){
        alert("请选择门店")
        return;
    }
    if(info.value.CouponType_Id==0){
        alert("请选择优惠券类型")
        return;
    }
    axios.post("https://localhost:7035/api/Coupon/AddCouponInfo",info.value)
    .then(res=>{
        if(res.data==-1){
            alert("优惠券名称已存在,请更改");
            return;
        }
        if(res.data>0){
            alert("添加成功")
            
        }else{
            alert("添加失败")
        }
    })
}


</script>